from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import json

# 连接到Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

# 打开包含要插入数据的JSON文件
with open('data.json', 'r', encoding='utf-8') as file:
    data = json.load(file)

# 构造批量插入的动作列表
actions = []
for item in data:
    action = {
        "_index": "your_index_name",
        "_id": item['id'],
        "_source": item
    }
    actions.append(action)

# 使用helpers模块执行批量插入操作
bulk(es, actions)